load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_python//python:defs.bzl", "py_test")
load("//:build/python_metadata.bzl", "BUNDLE_VERSION_INFO")
load("//:build/wd_cc_embed.bzl", "wd_cc_embed")
load(":helpers.bzl", "pyodide_extra", "pyodide_static", "python_bundles")

pyodide_extra()

python_bundles()

pyodide_static()

DEV_VERSION = BUNDLE_VERSION_INFO["development"]["real_pyodide_version"]

alias(
    name = "pyodide.capnp.bin",
    actual = "pyodide.capnp.bin@rule@" + DEV_VERSION,
    visibility = ["//visibility:public"],
)

wd_cc_embed(
    name = "python-entrypoint",
    src = "python-entrypoint.js",
    base_name = "python-entrypoint",
    visibility = ["//visibility:public"],
)

# In case we have a runner which doesn't support building pyodide.capnp.bin we
# want to use a pre-built/cross-compiled artifact. This is just a bazel alias
# serving as a redirect when referenced by other targets instead of changing
# the output filename.
alias(
    name = "pyodide.capnp.bin_cross",
    actual = select({
        "@//build/config:prebuilt_binaries_arm64": "@//:bin.arm64/tmp/pyodide/pyodide.capnp.bin.aarch64-linux-gnu",
        "//conditions:default": "pyodide.capnp.bin",
    }),
    visibility = ["//visibility:public"],
)

alias(
    name = "pyodide.capnp.bin@rule",
    actual = "pyodide.capnp.bin@rule@" + DEV_VERSION,
    visibility = ["//visibility:public"],
)

alias(
    name = "pyodide",
    actual = "pyodide@" + DEV_VERSION,
    visibility = ["//visibility:public"],
)

write_file(
    name = "bundle_version_info",
    out = "bundle_version_info.json",
    content = [json.encode(BUNDLE_VERSION_INFO)],
)

py_test(
    name = "unit-test-introspection",
    srcs = [
        "internal/introspection.py",
        "internal/test_introspection.py",
    ] + glob(["internal/workers-api/src/workers/*"]),
    main = "internal/test_introspection.py",
)
